package dao;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import po.City;
import po.DataSourceFactory;

/**
 * Create By IntelliJ IDEA
 *
 * @author: jsonor
 * @create-Time: 2017/10/17 8:21
 * @description: 对city表的数据库原始操作
 */
public class CityDao {
  private QueryRunner runner;

  public CityDao() {
    runner = new QueryRunner(DataSourceFactory.getDataSource());
  }

  /**
   * 查询数据表city中的所有数据
   *
   * @return 数据的集合对象
   */
  public List<City> findAll() throws SQLException {
    String sql = "select * from city";

    return runner.query(sql, new BeanListHandler<City>(City.class));
  }

  /**
   * 通过id将city中对应的数据项删除
   *
   * @param id 被删除信息的id
   * @return 被操作的数据的数数量
   */
  public int deleteSingleById(int id) throws SQLException {
    String sql = "delete from city where id = ?";

    return runner.update(sql, id);
  }

  /**
   * 通过id 查询city
   *
   * @param id 需要查询的city的id
   * @return city数据
   */
  public City findById(int id) throws SQLException {
    String sql = "select * from city where id = ?";

    return runner.query(sql, new BeanHandler<City>(City.class), id);
  }
}
